#!/bin/bash

# 定义输出HTML文件
OUTPUT_FILE="network_info_enhanced.html"

# 开始生成HTML
cat > $OUTPUT_FILE <<EOF
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网络信息报告</title>
    <style>
        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            margin: 20px;
            line-height: 1.6;
        }
        h1 {
            color: #333;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
            text-align: center;
        }
        h2 {
            color: #444;
            margin-top: 30px;
            border-left: 5px solid #0066cc;
            padding-left: 10px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #0066cc;
            color: white;
            position: sticky;
            top: 0;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        tr:hover {
            background-color: #e6f7ff;
        }
        pre {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
            border-left: 4px solid #0066cc;
        }
        .interface-type {
            font-weight: bold;
            padding: 3px 6px;
            border-radius: 3px;
            font-size: 0.9em;
        }
        .physical { background-color: #e6ffe6; color: #006600; }
        .bridge { background-color: #e6e6ff; color: #0000cc; }
        .virtual { background-color: #f5e6ff; color: #660066; }
        .veth { background-color: #fff2e6; color: #cc6600; }
        .loopback { background-color: #e6f3ff; color: #0066cc; }
        .other { background-color: #f0f0f0; color: #333; }
        .summary {
            background-color: #f8f8f8;
            padding: 15px;
            border-radius: 5px;
            margin: 20px 0;
            border-left: 4px solid #0066cc;
        }
    </style>
</head>
<body>
    <h1>网络信息报告</h1>
    <div class="summary">
        <p><strong>生成时间：</strong>$(date "+%Y年%m月%d日 %H:%M:%S")</p>
        <p><strong>主机名：</strong>$(hostname)</p>
    </div>
    
    <h2>网络接口概览</h2>
    <table>
        <thead>
            <tr>
                <th>类型</th>
                <th>接口名称</th>
                <th>状态</th>
                <th>IP地址</th>
                <th>网关</th>
                <th>MAC地址</th>
                <th>主接口</th>
            </tr>
        </thead>
        <tbody>
EOF

# 获取网络接口信息并添加到HTML
ip -o link show | awk -F': ' '{print $2}' | while read iface; do
    # 获取接口状态
    state=$(ip -o link show $iface | awk '{print $9}')
    [ "$state" == "UP" ] && state="<span style='color:green'>已启用</span>"
    [ "$state" == "DOWN" ] && state="<span style='color:red'>已禁用</span>"
    
    # 获取MAC地址
    mac=$(ip -o link show $iface | awk '{print $17}')
    [ -z "$mac" ] && mac="无"
    
    # 获取主接口（如果有）
    master=$(ip -o link show $iface | grep -oP 'master \K\w+' || echo "无")
    
    # 确定接口类型
    if [[ $iface == lo ]]; then
        type_class="loopback"
        type="回环"
    elif [[ $iface =~ ^virbr ]]; then
        type_class="virtual"
        type="虚拟网桥"
    elif [[ $iface =~ ^vnet ]]; then
        type_class="veth"
        type="虚拟以太网"
    elif [[ $iface =~ ^br ]]; then
        type_class="bridge"
        type="网桥"
    elif [[ $iface =~ ^(eno|ens|eth) ]]; then
        type_class="physical"
        type="物理网卡"
    else
        type_class="other"
        type="其他"
    fi
    
    # 获取所有IPv4地址
    ipv4_addrs=$(ip -o -4 addr show $iface | awk '{print $4}' | tr '\n' ' ')
    [ -z "$ipv4_addrs" ] && ipv4_addrs="无"
    
    # 获取网关信息
    gateway=$(ip route | grep "default via.*dev $iface" | awk '{print $3}' | head -n1)
    [ -z "$gateway" ] && gateway="无"
    
    # 添加到HTML表格
    cat >> $OUTPUT_FILE <<EOF
            <tr>
                <td><span class="interface-type ${type_class}">$type</span></td>
                <td>$iface</td>
                <td>$state</td>
                <td>${ipv4_addrs%% }</td>
                <td>$gateway</td>
                <td>$mac</td>
                <td>$master</td>
            </tr>
EOF
done

# 添加详细的接口信息
cat >> $OUTPUT_FILE <<EOF
        </tbody>
    </table>
    
    <h2>详细接口信息</h2>
    <pre>
$(ip -d addr show)
    </pre>
    
    <h2>路由表信息</h2>
    <pre>
$(ip route)
    </pre>
    
    <h2>网桥信息</h2>
    <pre>
$(brctl show 2>/dev/null || echo "没有安装brctl工具，无法显示网桥信息")
    </pre>
    
    <h2>ARP表</h2>
    <pre>
$(ip neigh)
    </pre>
    
    <h2>网络连接统计</h2>
    <pre>
$(ss -s)
    </pre>
    
    <div class="summary">
        <h3>报告说明</h3>
        <p>本报告由系统自动生成，包含以下网络信息：</p>
        <ul>
            <li>所有网络接口的概览信息</li>
            <li>每个接口的详细配置</li>
            <li>系统路由表</li>
            <li>网桥配置信息</li>
            <li>ARP缓存表</li>
            <li>网络连接统计</li>
        </ul>
    </div>
</body>
</html>
EOF

# 优化HTML文件格式
if command -v xmllint &> /dev/null; then
    xmllint --format --output $OUTPUT_FILE $OUTPUT_FILE 2>/dev/null
fi

echo "网络信息报告已生成: $OUTPUT_FILE"
